// 获取用户的当前设置
function getSetting({
  withSubscriptions = false,
  isShowLoading = true
} = {}) {
  if (isShowLoading) {
    showLoading();
  }
  let pomise = new Promise((resolve, reject) => {
    wx.getSetting({
      withSubscriptions: withSubscriptions,
      success: res => {
        if (isShowLoading) {
          hideLoading();
        }
        resolve(res);
      },
      fail: err => {
        if (isShowLoading) {
          hideLoading();
        }
        reject(err);
      }
    });
  });
  return pomise;
}

// 提前向用户发起授权请求
function authorize({
  scope,
  isShowLoading = true
} = {}) {
  if (isShowLoading) {
    showLoading();
  }
  let pomise = new Promise((resolve, reject) => {
    wx.authorize({
      scope: scope,
      success: res => {
        if (isShowLoading) {
          hideLoading();
        }
        resolve(res);
      },
      fail: err => {
        if (isShowLoading) {
          hideLoading();
        }
        reject(err);
      }
    });
  });
  return pomise;
}

// 获取当前的地理位置
function getLocation({
  type = 'wgs84',
  altitude = 'false',
  isHighAccuracy = false,
  highAccuracyExpireTime,
  isShowLoading = true
} = {}) {
  if (isShowLoading) {
    showLoading();
  }
  let pomise = new Promise((resolve, reject) => {
    wx.getLocation({
      type: type,
      altitude: altitude,
      isHighAccuracy: isHighAccuracy,
      highAccuracyExpireTime: highAccuracyExpireTime,
      success: res => {
        if (isShowLoading) {
          hideLoading();
        }
        resolve(res);
      },
      fail: err => {
        if (isShowLoading) {
          hideLoading();
        }
        reject(err);
      }
    });
  });
  return pomise;
}

// 打开地图选择位置
function chooseLocation({
  latitude,
  longitude,
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.chooseLocation({
      latitude: latitude,
      longitude: longitude,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 显示模态对话框
function showModal({
  title = '提示',
  content,
  showCancel = true,
  cancelText = '取消',
  cancelColor = '#000000',
  confirmText = '确定',
  confirmColor = '#576B95',
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.showModal({
      title: title,
      content: content,
      showCancel: showCancel,
      cancelText: cancelText,
      cancelColor: cancelColor,
      confirmText: confirmText,
      confirmColor: confirmColor,
      success: res => {
        if (res.confirm) {
          resolve(res);
        } else if (res.cancel) {
          reject(res);
        }
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 显示消息提示框
function showToast({
  title,
  icon = "none",
  image,
  duration = 1500,
  mask = true
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.showToast({
      title: title,
      icon: icon,
      image: image,
      duration: duration,
      mask: mask,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

//显示 loading 提示框 showLoading
function showLoading({
  title = '正在加载...',
  mask = true
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.showLoading({
      title: title,
      mask: mask,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

//显示 loading 提示框 showLoading
function hideLoading() {
  let pomise = new Promise((resolve, reject) => {
    wx.hideLoading({
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 调起客户端小程序订阅消息界面
function requestSubscribeMessage({
  tmplIds = []
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.requestSubscribeMessage({
      tmplIds: tmplIds,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

//调起客户端小程序设置界面
function openSetting({
  withSubscriptions = false
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.openSetting({
      withSubscriptions: withSubscriptions,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

//检查登录态是否过期 
function checkSession() {
  let pomise = new Promise((resolve, reject) => {
    wx.checkSession({
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

//调用接口获取登录凭证 
function login({
  timeout
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.login({
      timeout: timeout,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 使用微信内置地图查看位置
function openLocation({
  latitude,
  longitude,
  scale = 18,
  name,
  address,
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: scale,
      name: name,
      address: address,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

//Socket

// 创建一个 WebSocket 连接
function connectSocket({
  url,
  header,
  protocols,
  tcpNoDelay,
  perMessageDeflate,
  timeout
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.onSocketOpen({
      url: url,
      header: header,
      protocols: protocols,
      tcpNoDelay: tcpNoDelay,
      perMessageDeflate: perMessageDeflate,
      timeout: timeout,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 关闭 WebSocket 连接
function socketTaskClose(task, {
  code,
  reason
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    task.close({
      code: code,
      reason: reason,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 通过 WebSocket 连接发送数据
function socketTaskSend(task, {
  data
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    task.send({
      data: data,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 获取网络类型
function getNetworkType() {
  let pomise = new Promise((resolve, reject) => {
    wx.getNetworkType({
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}
module.exports = {
  getSetting,
  openSetting,
  authorize,
  getLocation,
  chooseLocation,
  showModal,
  showToast,
  showLoading,
  hideLoading,
  requestSubscribeMessage,
  checkSession,
  login,
  openLocation,
  connectSocket,
  socketTaskSend,
  socketTaskClose,
  getNetworkType,
}